package com.pzhu.record.controller;

import com.pzhu.record.constants.ExcelReader;
import com.pzhu.record.constants.ExcelWriter;
import com.pzhu.record.entity.Achievement;
import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Course;
import com.pzhu.record.entity.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @author Zhou YuSen
 * @date 2020/4/10 14:39
 */
@Controller
@RequestMapping("/achievement")
public class AchievementController extends BaseAction {

    /**
     * 批量添加学生信息数据
     *
     * @param score
     * @param model
     * @return
     */
    @RequestMapping("/insertExcel")
    public String insertExcel(MultipartFile score, Model model) {
        ExcelReader<Achievement> achievementExcelReader = new ExcelReader<>();
        List<Achievement> achievementList = achievementExcelReader.readExcel(score, new Achievement());
        System.out.println(achievementList);
        for (Achievement list : achievementList) {
            Basicinformation basicinformation = basicinformationService.selectById(list.getBasicinformationId());
            if (basicinformation == null) {
                model.addAttribute("messageAchievement", "添加失败");
                return "nav";
            }
            System.out.println(list.getBasicinformationId());
        }
        int insertBatch = achievementService.insertBatch(achievementList);

        if (insertBatch == 0) {
            model.addAttribute("messageAchievement", "添加失败");
            return "nav";
        }
        model.addAttribute("messageAchievement", "添加成功");
        return "nav";
    }

    /**
     * 分页条件查询
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectLimit")
    public String selectPageUseDyc(HttpServletRequest request, Model model, HttpSession session) throws ParseException {
        int pageIndex = 0;
        String keywordTime = request.getParameter("keywordTime");
        String keywordCouse = request.getParameter("keywordCouse");
        String keywordClass = request.getParameter("keywordClass");
        String keywordName = request.getParameter("keywordName");
        Course course = new Course();
        Basicinformation basicinformation = new Basicinformation();
        if (!"".equals(keywordTime) && keywordTime != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            course.setCourseYear(format.parse(keywordTime));
        }
        if (!"".equals(keywordCouse) && keywordCouse != null) {
            keywordCouse = "%" + keywordCouse + "%";
            course.setCourseCoursename(keywordCouse);
        }
        if (!"".equals(keywordClass) && keywordClass != null) {
            keywordClass = "%" + keywordClass + "%";
            basicinformation.setBasicinformationClass(keywordClass);
        }
        if (!"".equals(keywordName) && keywordName != null) {
            keywordName = "%" + keywordName + "%";
            basicinformation.setBasicinformationName(keywordName);
        }
        Achievement achievement = new Achievement();
        achievement.setCourse(course);
        achievement.setBasicinformation(basicinformation);
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }

        Page<Achievement> achievementPage = achievementService.selectPage(pageIndex, achievement);
        System.out.println(achievementPage);

        model.addAttribute("achievementPage", achievementPage);

        return "nav";
    }

    /**
     * 分页条件查询,学生查询
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectLimitStu")
    public String selectPageUseDycStu(HttpServletRequest request, Model model, HttpSession session) throws ParseException {
        int pageIndex = 0;

        Course course = new Course();

        Basicinformation basicinformation = (Basicinformation) session.getAttribute("userInfo");
        Achievement achievement = new Achievement();
        achievement.setCourse(course);
        achievement.setBasicinformation(basicinformation);
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }

        Page<Achievement> achievementPage = achievementService.selectPage(pageIndex, achievement);

        model.addAttribute("achievementPage", achievementPage);

        return "nav";
    }

    /**
     * 导出信息
     *
     * @return
     */
    @RequestMapping(value = "/selectExport")
    public String selectExport(@RequestParam Map<String, String> keywords, HttpServletResponse response) {

        if (keywords.size() != 0) {
            for (String value : keywords.keySet()) {
                if ("".equals(keywords.get(value))) {
                    keywords.put(value, "%" + keywords.get(value) + "%");
                    System.out.println("111" + keywords);
                }
            }
        }
        List<Achievement> achievementList = achievementService.selectExport(keywords);
        String excelName = "导出学生成绩表";
        System.out.println(achievementList);
        ExcelWriter<Achievement> excelDataVoExcelWriter = new ExcelWriter<Achievement>();
        excelDataVoExcelWriter.export(achievementList, excelName, response, new Achievement());

        return "nav";
    }

    /**
     * 导出信息 学生
     *
     * @return
     */

    @RequestMapping(value = "/selectExportStu")
    public String selectExportStu(@RequestParam Map<String, String> keywords, HttpServletResponse response, HttpSession session) {

        Basicinformation basicinformation1 = (Basicinformation) session.getAttribute("userInfo");
        keywords.put("keywordBasicinformationId", basicinformation1.getBasicinformationId());
        List<Achievement> achievementList = achievementService.selectExport(keywords);
        String excelName = "导出学生成绩表";
        ExcelWriter<Achievement> excelDataVoExcelWriter = new ExcelWriter<Achievement>();
        excelDataVoExcelWriter.export(achievementList, excelName, response, new Achievement());

        return "nav";
    }

}
